var recomDetails={
	zan: $('.zan'),
	zanNum: $('.zan-num'),
	numMax: 20,
	numMin: 1,
	cartE: $('button'),
	init: function(){
		this.createZan(20);
	},
	createZan: function(num){		
		var n=Math.floor(Math.random()*((this.numMax-this.numMin+1)+this.numMin));
		this.zanNum.text(n);
	},
	clickLike: function(id){
		var t=$('.zan');
		var tm=$('.zan-num');
		if(t.find('img').attr('src').indexOf('love-active') >0){
			app.ajax(
			'/Goods/Index/clear_zan',
			{
				skuid :id
			},function(res){
				if(res.code == 200){
				t.find('img').attr('src','../img/product/love.png');
				tm.text(res.thing_num);
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			});
		}else{
			app.ajax(
			'/Goods/Index/zan',
			{
				skuid :id
			},function(res){
				console.log(JSON.stringify(res));
				if(res.code == 200){
					t.find('img').attr('src','../img/product/love-active.png');
					tm.text(res.thing_num);
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			});

		}
	},
	clickCart: function(){
		var id=$('.detail-title').attr('data-id'),
		num=1,
		warehouseType=$('.detail-title').attr('data-warehouseType'),
		supplierId=$('.detail-title').attr('data-supplierId');
		app.ajax(
		'/Goods/Cart/add_cart',
		{
			skuid :id,
			buyPoductNum: num,
			warehouseType: warehouseType,
			supplierId: supplierId
		},function(res){
			if(res.code == 200){
				$('footer .index-cart i').animate({
					width:20,
					height:20,
					right:-5,
					top:7,
					lineHeight:'20px'
				},500);

				$('footer .index-cart i').animate({
					width:17,
					height:17,
					right:-7,
					top:10,
					lineHeight:'17px'
				},500);
				$('footer .index-cart i').delay(1000).text(res.num);
				var stock = parseInt($('.detail-title').attr('data-stock'));
				stock--;
				$('.detail-title').attr('data-stock',stock);
			}
		});
		
	}
};

var data=window.sessionStorage.getItem('recomDetail');
data=JSON.parse(data).id;
app.ajax(
	'/Goods/Index/goods_view',
	{
		skuid : data
	},
	function(res){
		if(res.code == 200){
			app.tpl($('.detail'),'tpl-detail',res);
			!res.info.video && $('.play').hide();
			var imgs=res.info.detail;
			if(imgs){
				imgs=imgs.split('/>');
				console.log('第一张图片：'+imgs[0]);
				$('.detail-img.n-first').html(imgs[0]+'/>');
				var html1='';
				for(var i=1;i<imgs.length;i++){
					if(i== imgs.length-1){
						html1+=imgs[i];
					}else{
						html1+=imgs[i]+'/>';
					}

				}
				$('.detail-img.n-all').html(html1);
			}
			$('.roundLike').text(parseInt($('.zan-num').text())+parseInt($('.info-sale span').text()));
		}else{
			layer.msg('系统繁忙，请稍候再试');
		}
	}
);
recomDetails.init();
header.init('详情');
window.onload=function(){
	
	setTimeout(function(){
		var myswiper1 = new Swiper('.swiper-container', {
	      autoplay: 3000,
	      pagination:'.swiper-pagination',
	      paginationClickable: true,
	      autoplayDisableOnInteraction : false,
	   	  loop:true
	 	});
	},1000);


	app.toHistory(window.location.href);
	
	$('body').on('click','.zan',function(){
		recomDetails.clickLike(data);
	});
	
	$('body').on('click','.addCart',function(){
		var stock = parseInt($('.detail-title').attr('data-stock'));
		if(stock >1){
			recomDetails.clickCart();
		}else{
			alert('库存不足');
		}
	});
	$('body').on('click','.add-more',function(){
		$('.detail-img.n-all').show();
		$(this).remove();
	});
	
	/**去结算*/
    $('body').on('click','footer .toBuy',function(){
    	var id = $('.pSR .detail-title').attr('data-id'),
    	stock = parseInt($('.detail-title').attr('data-stock')),
    	data={},
    	warehouseType=$('.pSR .detail-title').attr('data-warehouseType'),
    	supplierId = $('.pSR .detail-title').attr('data-supplierId');
    	data = {
    		supplier_id : supplierId,
			warehouse_type :warehouseType,
			skuIds :id,
			active_id : 3//  活动  3
    	};
		if(stock >1){
			footerD.buy(data);
		}else{
			layer.msg('库存不足');
		}
		
	});
}